package com.sdtv.mediunfusion.mvp.ui.home;

import android.content.Intent;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.basemodule.config.life.AppManager;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.mvp.ui.home.fragment.IndexFragment;
import com.sdtv.mediunfusion.mvp.ui.home.fragment.PersonFragment;
import com.sdtv.mediunfusion.mvp.ui.home.fragment.PunchFragment;
import com.sdtv.mediunfusion.utils.RouterPath;
import com.sdtv.mediunfusion.widget.CustomeToolBar;
import com.sdtv.mediunfusion.widget.CustomerDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RouterPath.TO_MAINACTIVITY)
public class HomeActivity extends BaseActivity {

    @BindView(R.id.nav_view)
    BottomNavigationView navView;
    @BindView(R.id.contentLayout)
    FrameLayout contentLayout;
    @BindView(R.id.home_cusBar)
    CustomeToolBar toolBar;
    // fragment集合
    private List<Fragment> fragmentList;
    //当前选中的标签
    private int currentSelectTab = 0;
    //设置页面 --主页
    private IndexFragment indexFragment;
    // 打卡页面
    private PunchFragment punchFragment;
    // 个人中心
    private PersonFragment personFragment;
    // 弹出框
    private CustomerDialog exitDialog;
    /**
     * 导航切换事件
     */
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = item -> {
        switch (item.getItemId()) {
            // 主页
            case R.id.navigation_home:
                if (currentSelectTab != 0) {
                    currentSelectTab = 0;
                    switchPage();
                }
                return true;
            // 打卡页面
            case R.id.navigation_dashboard:
                if (currentSelectTab != 1) {
                    currentSelectTab = 1;
                    switchPage();
                }
                return true;
            case R.id.navigation_notifications:
                //判断该页面是否已经加载过了
                // 如果未下载 则加载数据 如果已经加载了则直接显示
                if (currentSelectTab != 2) {
                    currentSelectTab = 2;
                    switchPage();
                }
                return true;
        }
        return false;
    };

    /**
     * 设置布局文件
     *
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_home;
    }

    /**
     * 初始化页面布局
     * <p> 创建首页布局
     * 将底部导航与fragment绑定
     */
    @Override
    public void initView() {
        // 构造页面
        fragmentList = new ArrayList<>();
        // 首页
        indexFragment = IndexFragment.getInstance();
        // 打卡页面
        punchFragment = PunchFragment.getInstance();
        // 个人中心页面
        personFragment = PersonFragment.getInstance();
        fragmentList.add(indexFragment);
        fragmentList.add(punchFragment);
        fragmentList.add(personFragment);
        // 设置导航绑定事件
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    /**
     * 加载页面数据开始 ---
     */
    @Override
    public void initData() {
        // 初始化--显示第一个标签
        getSupportFragmentManager().beginTransaction().
                replace(R.id.contentLayout, indexFragment).commitAllowingStateLoss();
    }

    /**
     * 切换页面显示
     */
    private void switchPage() {
        //设置导航标题
        switch (currentSelectTab) {
            case 0:
                toolBar.setTitle(getString(R.string.home_title));
                //显示电话按钮
                toolBar.showPhoneImg(true);
                toolBar.showRightTab(false);
                break;
            case 1:
                toolBar.setTitle(getString(R.string.punch_title));
                //显示右侧记录按钮
                toolBar.showRightTab(true);
                toolBar.showPhoneImg(false);
                break;
            case 2:
                toolBar.setTitle(getString(R.string.person_title));
                // 隐藏标签按钮
                toolBar.showPhoneImg(false);
                toolBar.showRightTab(false);
                break;
            default:
        }
        // 切换页面
        getSupportFragmentManager().beginTransaction().
                replace(R.id.contentLayout, fragmentList.get(currentSelectTab)).
                commitAllowingStateLoss();
    }

    /**
     * 返回绑定事件
     * <p>
     * -- 如果当前不是主页 则返回主页
     * 如果是主页 则提示 是否要退出应用
     */
    @Override
    public void onBackPressed() {
        if (0 != currentSelectTab) {
            //返回到首页
            navView.setSelectedItemId(R.id.navigation_home);
        } else {
            if (null == exitDialog) {
                exitDialog = new CustomerDialog.Builder().with(this)
                        .view(R.layout.dialog_exit)
                        .style(R.style.dialog_Fullscreen)
                        .addViewOnclick(R.id.ae_exitSure, view -> {
                            exitDialog.dismiss();
                            //销毁
                            indexFragment = null;
                            punchFragment = null;
                            personFragment = null;
                            AppManager.getInstance().exitApp();
                        })
                        .addViewOnclick(R.id.ae_exitNo, view -> {
                            exitDialog.dismiss();

                        }).build();

            }
            //当前已经是主页了 提示用户是否退出
            exitDialog.show();
        }
    }

    /**
     * 执行销毁事件
     * -- 销毁释放地图资源
     */
    @Override
    protected void onDestroy() {
        if (null != punchFragment) {
            punchFragment.releaseMap();
        }
        super.onDestroy();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (currentSelectTab == 1 && null != punchFragment) {
            // 刷新打卡记录
            punchFragment.onActivityResult(requestCode, resultCode, data);

        }
    }
}
